package dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import dao.EmployeeDao;
import entity.Department;
import entity.Employee;

public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {

	public Employee login(Employee employee) {
		String hql = "from Employee where username=? and password=?";
		List<Employee> list =  this.getHibernateTemplate().find(hql, employee.getUsername(), employee.getPassword());
		if(list.size()>0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public List<Employee> queryAll() {
//		Session session = sessionFactory.openSession();
//		//Classes
//		List<Classes> classes = session.createQuery("from Classes").list();
//		session.close();
		return this.getHibernateTemplate().find("from Employee");		
	}

	public int calCount() {
		List<Long> list = this.getHibernateTemplate().find("select count(*) from Employee");
		return list.get(0).intValue();
	}

	public List<Employee> findByPage(int begin, int pageSize) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class);
		return this.getHibernateTemplate().findByCriteria(criteria, begin, pageSize);
	}

	public void save(Employee employee) {
		this.getHibernateTemplate().save(employee);
	}

	public Employee findById(Integer eid) {
		return this.getHibernateTemplate().get(Employee.class, eid);
	}

	public void delete(Employee employee) {
		this.getHibernateTemplate().delete(employee);
	}

	public void update(Employee employee) {
		this.getHibernateTemplate().update(employee);
	}

}
